#pragma once
#include "CInterlockedReferenceCount.h"
#include "HWVertexObject.h"
#include "HWInstanceInfoPersistent.h"
#include "HWIndependentBatch.h"
#include "CachedHWMediaBatchInfo.h"

namespace PrimitiveCompositor
{
	enum Flags
	{
		None = 0,
		EnableBatchedDrawing = 1,
		ForceShapeMaskedPrimitives = 2,
		ForceDWORD = 0xff,
	};
};

class HWFrameKeyedMutexBuffer;


class HWCrossFrameResources :
	public CInterlockedReferenceCount
{
public:
	struct HWVertexObject s_unitRectangleVertices[8];


private: 
	static const XUINT32 s_defaultInstanceCount;
	static const XUINT32 s_defaultGeneratedVertexCount;
public: 
	static HRESULT Create(HWCrossFrameResources * * ppHwCrossFrameResources);
	XUINT32 CheckPrimitiveCompositorFlags(enum PrimitiveCompositor::Flags flagsToCheck);
	HWFrameKeyedMutexBuffer * GetHWFrameKeyedMutexBuffer();
	void SetHWFrameKeyedMutexBuffer(HWFrameKeyedMutexBuffer * pHWFrameKeyedMutexBuffer);
	HRESULT EnsureHardwareResources(struct IPALOptimizedGraphicsDevice * pGraphicsDevice, XUINT32 objectCount, XUINT32 generatedVertexCount);
	void HandleDeviceLost();
	void FreeBuffers();

private: 
	HWCrossFrameResources();
	virtual ~HWCrossFrameResources();
	HRESULT Initialize();
	HRESULT GenerateIndices(struct IPALOptimizedGraphicsDevice * pGraphicsDevice, XUINT32 objectCount, enum IndexBufferType type);
	HRESULT GenerateUnitRectangle(struct IPALOptimizedGraphicsDevice * pGraphicsDevice);
	struct IPALGraphicsDeviceBuffer * m_pUnitRectangleBuffer;
	struct IPALGraphicsDeviceBuffer * m_pInstanceBuffer;

private: 
	struct IPALGraphicsDeviceBuffer * m_pIndexBuffers[3];
	struct IPALGraphicsDeviceBuffer * m_pGeometryGenerationVertexBuffer;
	struct IPALSurface * m_pWhitePixel;
	unsigned int m_instanceCapacity;
	unsigned int m_generatedVertexCapacity;
	struct IPALCriticalSection * m_pObjectDirtyLock;
	xvector<HWInstanceInfoPersistent> m_independentInstances; 
	xvector<HWIndependentBatch> m_independentBatches; 
	xvector<CachedHWMediaBatchInfo> m_cachedMediaBatchInfo; 
	unsigned int m_primitiveCompositionModeFlags;
	HWFrameKeyedMutexBuffer * m_pHWFrameKeyedMutexBuffer;

};
